package cloud.shyidong.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/10/18
 */
@Component
@Slf4j
@RefreshScope
public class AuthFilter implements GlobalFilter {
    
    //    private AuthServiceFeign authServiceFeign;
//
//    @Autowired
//    public void setAuthServiceFeign(AuthServiceFeign authServiceFeign) {
//        this.authServiceFeign = authServiceFeign;
//    }
//
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        
        ServerHttpRequest serverHttpRequest = exchange.getRequest();
        RequestPath requestPath = serverHttpRequest.getPath();
        log.info("path >>> {}", requestPath);
        
        List<PathContainer.Element> elements = requestPath.elements();
        if (CollectionUtils.isEmpty(elements) || elements.size() < 2) {
            log.info("elements is empty");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        
        //认证服务
        if ("cloud-auth".equals(elements.get(1).value())) {
            return chain.filter(exchange);
        }
        //后台管理服务
        else if ("cloud-admin".equals(elements.get(1).value())) {
            HttpHeaders httpHeaders = serverHttpRequest.getHeaders();
            if (!httpHeaders.containsKey("token")) {
                log.info("admin token is not in headers");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }
            List<String> tokenList = httpHeaders.get("token");
            if (CollectionUtils.isEmpty(tokenList)) {
                log.info("admin token is empty");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            } else {
//                Result result = authServiceFeign.getAdminInfo(tokenList.get(0));
//                if (!result.isLogin()) {
//                    exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//                    return exchange.getResponse().setComplete();
//                }
                log.info("admin pass");
                return chain.filter(exchange);
            }
        }
        //租户服务
        else if ("cloud-tenant".equals(elements.get(1).value())) {
            HttpHeaders httpHeaders = serverHttpRequest.getHeaders();
            if (!httpHeaders.containsKey("tenant-token")) {
                log.info("tenant token is not in headers");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }
            List<String> tokenList = httpHeaders.get("tenant-token");
            if (CollectionUtils.isEmpty(tokenList)) {
                log.info("tenant token is empty");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            } else {
//                Result result = authServiceFeign.getTenantLoginInfo(tokenList.get(0));
//                if (!result.isLogin()) {
//                    exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//                    return exchange.getResponse().setComplete();
//                }
                log.info("tenant pass");
                return chain.filter(exchange);
            }
        }
        //会员服务
        else if ("cloud-member".equals(elements.get(1).value())) {
            HttpHeaders httpHeaders = serverHttpRequest.getHeaders();
            if (!httpHeaders.containsKey("member-token")) {
                log.info("member token is not in headers");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }
            List<String> tokenList = httpHeaders.get("member-token");
            if (tokenList == null || tokenList.isEmpty()) {
                log.info("member token is empty");
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            } else {
                log.info("member pass");
                return chain.filter(exchange);
            }
        }
        return chain.filter(exchange);
    }
    
}
